package com.app.web.base.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.event.AjaxBehaviorEvent;

import org.primefaces.component.selectbooleancheckbox.SelectBooleanCheckbox;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.TreeNode;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.app.web.base.model.Resource;
import com.app.web.base.model.Role;
import com.app.web.base.model.User;
import com.app.web.base.service.ResourceService;
import com.app.web.base.util.Permission;
import com.app.web.base.util.controller.AppAttributes;
import com.app.web.base.util.controller.PageDateModel;
import com.app.web.base.vo.RoleResourceVO;
import com.app.web.base.vo.UserResourceVO;


/**
 * Project: erp<br>
 * Description: 资源Backing Bean。<br>
 * Copyright (c) 2012<br>
 * All Rights Reserved.<br>
 * @author <a href="mailto:gly420@163.com">GuanLuYong</a><br>
 */
@Controller @Scope("view")
public class ResourceBean extends BaseBean<Resource> implements Serializable {
	private static final long serialVersionUID = 1L;

	@javax.annotation.Resource
	private ResourceService resourceService;
	@javax.annotation.Resource
	private AppInfo appInfo;
	
	// table 树结构
	private TreeNode root;
	// 当前选中的资源
	private TreeNode selectedRes;
	// 类型
	private String rootType;
	// 父类型资源列表
	private List<Resource> parentResources;
	
	// 角色-资源 
	private LazyDataModel<RoleResourceVO> lazyRoleResData;
	private RoleResourceVO[] roleReses;
	private List<RoleResourceVO> roleResList;
	// 按条件查找role
	private LazyDataModel<Role> lazyRole;
	private Role[] roles;
	
	// 用户-资源
	private List<UserResourceVO> users;
	private LazyDataModel<UserResourceVO> lazyUserResData;
	private UserResourceVO[] userReses;
	private UserResourceVO userResourceVO;
	
	@PostConstruct public void initResourceBean() {
		root = new DefaultTreeNode("root", null);
		rootType = AppAttributes.RESOURCE_TYPE;
		parentResources = getResourcesByParentId(0l);
		
		roleResList = new ArrayList<RoleResourceVO>();
		users = new ArrayList<UserResourceVO>();
	}
	public void searchTreetable() {
		root.getChildren().clear();
		resourceService.buildTree(root, queryMap);
	}
	/**
	 * 新增资源，初始化
	 */
	public void initAddResource() {
		instance = new Resource();
		instance.setStatus(1l);
		
		parentResources = getResourcesByParentId(0l);
	}
	
	public List<Resource> getResourcesByParentId(Long parentId) {
		return resourceService.getResourcesByParentId(parentId);
	}
	/**
	 * 获得当前资源的所有角色和权限
	 */
	public void getResRoles() {
		roleResList.clear();
		List<RoleResourceVO> rrvs = resourceService.getRolesByResId(instance.getId());
		List<Role> roles = resourceService.getAllRoles();
		for(Role r : roles) {
			RoleResourceVO rv = null;
			for(RoleResourceVO rrv : rrvs) {
				if(r.getId().compareTo(rrv.getId()) == 0) {
					rv = rrv;
					break;
				}
			}
			if(rv == null) {
				rv = new RoleResourceVO(r, new Permission());
			}
			roleResList.add(rv);
		}
		lazyRoleResData = new PageDateModel<RoleResourceVO>(roleResList);
	}

	/**
	 * 关联权限和角色
	 * 
	 */
	public void communicateRoleRes() {
		resourceService.communicateRoleRes(roleResList, instance);
		opSuccess();
	}
	
	/**
	 * 取当前资源所有用户
	 */
	public void getResUsers() {
		users.clear();
		for(UserResourceVO urv : resourceService.getUsersByResId(instance.getId())) {
			users.add(urv);
		}
		lazyUserResData = new PageDateModel<UserResourceVO>(users);
	}
	public void chooseUsers(User[] us) {
		for(User u : us) {
			boolean f = false;
			for(UserResourceVO urv : users) {
				if(u.getId().compareTo(urv.getUser().getId()) == 0) {
					f = true;
					break;
				}
			}
			if(!f) {
				users.add(new UserResourceVO(u, new Permission()));
			}
		}
	}
	public void communicateResUsers() {
		resourceService.communicateResUsers(users, instance);
		opSuccess();
	}
	
	public void checkPermIsAll(AjaxBehaviorEvent actionEvent) {
		SelectBooleanCheckbox box = (SelectBooleanCheckbox) actionEvent.getComponent();
		System.out.println(box.getValue());
		System.out.println(userResourceVO.getUser().getName());
	}
	
	@Override
	public void search() { }

	@Override
	public void editEntity() throws Exception {
		super.editEntity();
		resourceService.update(instance);
		appInfo.getAllDicts();
	}

	@Override
	public void addEntity() throws Exception {
		super.addEntity();
		resourceService.insert(instance);
		appInfo.getAllDicts();
	}

	@Override
	public void removeEntity() throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	// G & S
	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getSelectedRes() {
		return selectedRes;
	}

	public void setSelectedRes(TreeNode selectedRes) {
		this.selectedRes = selectedRes;
	}

	public String getRootType() {
		return rootType;
	}

	public List<Resource> getParentResources() {
		return parentResources;
	}

	public AppInfo getAppInfo() {
		return appInfo;
	}

	public void setAppInfo(AppInfo appInfo) {
		this.appInfo = appInfo;
	}

	public RoleResourceVO[] getRoleReses() {
		return roleReses;
	}

	public void setRoleReses(RoleResourceVO[] roleReses) {
		this.roleReses = roleReses;
	}

	public UserResourceVO[] getUserReses() {
		return userReses;
	}

	public void setUserReses(UserResourceVO[] userReses) {
		this.userReses = userReses;
	}

	public LazyDataModel<RoleResourceVO> getLazyRoleResData() {
		return lazyRoleResData;
	}

	public LazyDataModel<UserResourceVO> getLazyUserResData() {
		return lazyUserResData;
	}
	public LazyDataModel<Role> getLazyRole() {
		return lazyRole;
	}
	public void setLazyRole(LazyDataModel<Role> lazyRole) {
		this.lazyRole = lazyRole;
	}
	public Role[] getRoles() {
		return roles;
	}
	public void setRoles(Role[] roles) {
		this.roles = roles;
	}
	public List<UserResourceVO> getUsers() {
		return users;
	}
	public void setUsers(List<UserResourceVO> users) {
		this.users = users;
	}
	public UserResourceVO getUserResourceVO() {
		return userResourceVO;
	}
	public void setUserResourceVO(UserResourceVO userResourceVO) {
		this.userResourceVO = userResourceVO;
	}

}
